<?php
/*
Plugin Name: Request Proxy
Plugin URI: https://zengxiaoluan.com/request-proxy
Description: 请求转发
Version: 0.0.1
Author: zengxiaoluan
Author URI: https://zengxiaoluan.com/
License: GPL2+
License URI: https://www.gnu.org/licenses/gpl-2.0.txt
Text Domain: 
*/

add_action('wp_ajax_nopriv_get_rss_data', 'my_ajax_handler');
add_action('wp_ajax_get_rss_data', 'my_ajax_handler');

function my_ajax_handler()
{
  $feed = $_GET['feedUrl'];
  $response = wp_remote_get($feed);
  // var_dump($response);
  if (is_array($response) && !is_wp_error($response)) {
    $headers = $response['headers']; // array of http header lines
    $body = $response['body']; // use the content
    wp_send_json_success($body);
  } else {
    $message = $response->get_error_messages();
    wp_send_json_error($message);
  }
}

add_action('wp_head', 'myplugin_ajaxurl');
function myplugin_ajaxurl()
{
  echo '<script type="text/javascript">var ajaxurl = "' .
    admin_url('admin-ajax.php') .
    '";</script>
    ';
}
?>
